// pages/first/first.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        account: "",
        password: "",
        password_repeat: "",
        shop_name: "",
        shop_region: "",
        shop_address: "",
        human_name: "",
        human_phone: ""
    },

    /**
     * 页面准备就绪
     */
    onReady: function() {
        app.verifyLogin();
    },

    /**
     * 选择店铺地区
     */
    selectShopRegion: function(e) {
        let shop_region = '';
        for (let i = 0; i < e.detail.value.length; i++) {
            shop_region += e.detail.value[i];
        };
        this.setData({
            shop_region: shop_region,
        });
    },

    /**
     * 输入框键入字符
     */
    inputText: function(e) {
        let data = {};
        data[e.target.dataset.name] = e.detail.value.replace(/[\n\r]/g, '');
        this.setData(data);
    },

    /**
     * 提交
     */
    submitRegister: function() {

        let data = this.data;

        if (0 === data.account.length) {
            wx.showToast({
                title: "账号不能为空",
                icon: "none"
            });
            return;
        };
        if (6 > data.account.length) {
            wx.showToast({
                title: "账号不能小于6位",
                icon: "none"
            });
            return;
        };
        if (0 === data.password.length) {
            wx.showToast({
                title: "密码不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.password.length) {
            wx.showToast({
                title: "密码不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.password.length) {
            wx.showToast({
                title: "密码不能为空",
                icon: "none"
            });
            return;
        };
        if (6 > data.password.length) {
            wx.showToast({
                title: "密码不能小于6位",
                icon: "none"
            });
            return;
        };
        if (0 === data.password_repeat.length) {
            wx.showToast({
                title: "重复密码不能为空",
                icon: "none"
            });
            return;
        };
        if (data.password !== data.password_repeat) {
            wx.showToast({
                title: "密码与重复密码不同",
                icon: "none"
            });
            return;
        };
        if (0 === data.shop_name.length) {
            wx.showToast({
                title: "店铺名称不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.shop_region.length) {
            wx.showToast({
                title: "店铺地区不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.shop_address.length) {
            wx.showToast({
                title: "店铺地址不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.human_name.length) {
            wx.showToast({
                title: "联系人姓名不能为空",
                icon: "none"
            });
            return;
        };
        if (0 === data.human_phone.length) {
            wx.showToast({
                title: "联系人手机不能为空",
                icon: "none"
            });
            return;
        };
        if (11 !== data.human_phone.length) {
            wx.showToast({
                title: "联系人手机的长度为11位",
                icon: "none"
            });
            return;
        };

        wx.showToast({
            title: '处理中...',
            icon: "loading",
            duration: 60000,
            mask: true
        });
        wx.request({
            url: app.data.service_url + 'shop/register',
            header: {
                'Client-Token': app.data.client_token
            },
            data: data,
            method: 'POST',
            success: function(response) {
                wx.hideToast();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        wx.showToast({
                            title: response.data.message,
                        });
                        setTimeout(function () {
                            wx.redirectTo({
                                url: '/pages/base_mine/index/index',
                            });
                        }, 2000);
                    }else{
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                }else{
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideToast();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        })
    }
})